package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.enums.model.PaymentStatus;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.order.model.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.management.relation.RelationSupport;
import javax.servlet.http.HttpServletRequest;

/**
 * @author: atguigu
 * @create: 2023-09-19 10:25
 */
@Controller
public class PaymentHtmlController {

    @Autowired
    private OrderFeignClient orderFeignClient;

    /**
     * 渲染支付方式选择页面
     *
     * @param orderId
     * @return
     */
    @GetMapping("/pay.html")
    public String choosePayType(HttpServletRequest request, Model model, @RequestParam("orderId") Long orderId) {
        String userId = AuthContextHolder.getUserId(request);
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
        if (orderInfo == null) {
            throw new RuntimeException("订单不存在！");
        }
        if (!userId.equals(orderInfo.getUserId().toString())) {
            throw new RuntimeException("参数有误！");
        }
        if (!PaymentStatus.UNPAID.name().equals(orderInfo.getOrderStatus())) {
            throw new RuntimeException("订单状态有误！");
        }
        model.addAttribute("orderInfo", orderInfo);
        return "/payment/pay";
    }

    /**
     * 支付成功页面渲染
     * @return
     */
    @GetMapping("/pay/success.html")
    public String paySuccessHtml() {
        return "/payment/success";
    }
}
